package com.ithaipeng.order.service;

import com.ithaipeng.feign.pojo.User;
import com.ithaipeng.feign.clients.UserClient;
import com.ithaipeng.order.mapper.OrderMapper;
import com.ithaipeng.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;
    //使用RestTemplate
   /* @Autowired
    private RestTemplate restTemplate;
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2 利用RestTemplate发起http请求
        //2.1 url路径
        String url="http://userservice/user/"+order.getUserId();
        //发送请求，实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        //3:封装user到Order中
        order.setUser(user);
        // 4.返回
        return order;
    }*/

    //使用feign
    public Order queryOrderById(Long orderById){
        //1 查询订单
        Order order = orderMapper.findById(orderById);
        // 2用feign来远程调用
        User user = userClient.findById(order.getUserId());
        // 3封装user到Order
        order.setUser(user);
        return order;
    }
}
